package com.stx.blog.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.stx.blog.dao.UserDao;
import com.stx.blog.entity.UserEntity;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
   
	public UserEntity findUserBySnoAndSpassword(String username,String password) {
		JdbcTemplate jt = getJdbcTemplate();

		String sql = "select * from USERS where uname = ? and upassword = ?";

		
		Object[] args = { username, password };

		List<UserEntity> stuList = jt.query(sql, args,
				new UserRowMapper());

		UserEntity entity = null;

		if (stuList.isEmpty()) {
			return null;
		} else {
			entity = (UserEntity) stuList.get(0);
			return entity;
		}

	}
	
	class UserRowMapper implements RowMapper {
		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			
			UserEntity entity = new UserEntity();


			entity.setUname(rs.getString("uname"));
		
			entity.setUpassword(rs.getString("upassword"));
			
			return entity;
		}
	}
}
